package com.OnTimeTeam.OnTime;

import java.util.ArrayList;

import com.OnTimeTeam.OnTime.Track.SubTrackBus;
import com.OnTimeTeam.OnTime.Track.SubTrackWalk;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BinderData extends BaseAdapter {

	 
	LayoutInflater inflater;
	ImageView thumb_image;
	
	ArrayList<Track> tracks;
	ViewHolder holder;
	Context context;
	
	private static int mItemIndex;
	
	public BinderData() {
		// TODO Auto-generated constructor stub
	}
	
	public BinderData(Activity act, ArrayList<Track> tracks, Context ctx) {
	
		context = ctx;
		this.tracks = tracks;
		inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	

	public static void setSelectItem(int index) {
        mItemIndex = index;
    }

    public static int getSelectItem() {
        return mItemIndex;
    }

	public int getCount() {
		// TODO Auto-generated method stub
//		return idlist.size();
		return tracks.size();
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		 
		View vi=convertView;
	//	System.out.println("Start getView from adapter\n");
		if(convertView == null){
	     
	      vi = inflater.inflate(R.layout.track_item, null);
	      holder = new ViewHolder();
	     
	   //   System.out.println("after new ViewHolder\n");
	      
	      holder.tvFirst = (TextView)vi.findViewById(R.id.tvFirst); 
	      holder.tvSecond = (TextView)vi.findViewById(R.id.tvSecond);
	      holder.tvThird = (TextView)vi.findViewById(R.id.tvThird);
	     
	   //   System.out.println("after findViewById - Text\n");
	      //Need to change bus & walk names in track_item to generic names
	      //according to the relevant sub_track display the correct image + text
	      //Limit of 3 sub_tracks
	      holder.ivFirst =  (ImageView)vi.findViewById(R.id.ivFirst); 
	      holder.ivSecond =(ImageView)vi.findViewById(R.id.ivSecond); 
	      holder.ivThird =(ImageView)vi.findViewById(R.id.ivThird); 
	      holder.ivArrowFirst = (ImageView)vi.findViewById(R.id.imageView02);
	      holder.ivArrowSecond = (ImageView)vi.findViewById(R.id.imageView01);
	      holder.ivArrowHidden = (ImageView)vi.findViewById(R.id.imageView2);
	      
	      
	      holder.tvDepArrTime = (TextView)vi.findViewById(R.id.tvDepArrTime);
	      holder.tvDuration = (TextView)vi.findViewById(R.id.tvDuration);
	      
	      
	      vi.setTag(holder);
	      
	    //  System.out.println("after findViewById - Image and vi.setTag\n");
	    }
	    else{
	    	
	    	holder = (ViewHolder)vi.getTag();
	    }

	   
		// Setting all values in listview    
		//Need to implement dynamic view (if the order of walking-traveling is different...)		
		String uri;
		int imageResource;
		Drawable image;
		int subTracksNum = tracks.get(position).getSubTracks().size();
		
		//set background color as chosen route 
	    if (position == mItemIndex)  {
	    	//vi.setBackground(context.getResources().getDrawable(R.drawable.gradient_bg_hover));
	    	vi.setBackgroundColor(Color.parseColor("#0099CC"));
	    }
	    else {
            vi.getBackground().setAlpha(0);
	   
          //  vi.setBackgroundColor(context.getResources().getColor(android.R.color.darker_gray));
        //    vi.setBackgroundColor(Color.parseColor("#001e28"));
	    
	    }
	    
		if(Track.SubTrackWalk.class.isInstance(tracks.get(position).getSubTracks().get(0)))
		{
			Track.SubTrackWalk subTrackWalk1 = (SubTrackWalk) tracks.get(position).getSubTracks().get(0);
			holder.tvFirst.setText(subTrackWalk1.getDuration());	
			uri = "drawable/"+ "walk_transit";
			
		}
		else
		{
			Track.SubTrackBus subTrackBus1 = (SubTrackBus) tracks.get(position).getSubTracks().get(0);
			
			//subTrack is train...
			if(subTrackBus1.getLineShortName().equals(""))
			{
				holder.tvFirst.setText(subTrackBus1.getVehicleType());
				uri = "drawable/"+ "train_transit";			
			}
			else
			{
				holder.tvFirst.setText(context.getResources().getString(R.string.line) +" "+ subTrackBus1.getLineShortName());
				uri = "drawable/"+ "bus_transit";
			}
		//	System.out.println("after set holder text - track #" + position+1 + "subTrack #1 type Bus\n");
		}
		
		imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
	    image = vi.getContext().getResources().getDrawable(imageResource);
	    holder.ivFirst.setImageDrawable(image);
	 //   System.out.println("after set holder Image - track #" + position+1 + "subTrack #1 Bus/Walk\n");
	if(subTracksNum > 1)
	{
		if((Track.SubTrackWalk.class).isInstance(tracks.get(position).getSubTracks().get(1)))
		{
			Track.SubTrackWalk subTrackWalk2 = (SubTrackWalk) tracks.get(position).getSubTracks().get(1);
			holder.tvSecond.setText(subTrackWalk2.getDuration());
			uri = "drawable/"+ "walk_transit";
			//System.out.println("after set holder Text- track #" + position+1 + "subTrack #2 type Walk\n");
		}
		else 
		{
			Track.SubTrackBus subTrackBus2 = (SubTrackBus) tracks.get(position).getSubTracks().get(1);
			if(subTrackBus2.getLineShortName().equals(""))
			{
				holder.tvSecond.setText(subTrackBus2.getVehicleType());
				uri = "drawable/"+ "train_transit";			
			}
			else
			{
				holder.tvSecond.setText(context.getResources().getString(R.string.line) +" "+ subTrackBus2.getLineShortName());
				uri = "drawable/"+ "bus_transit";
			}
			//System.out.println("after set holder Text- track #" + position+1 + "subTrack #2 type Bus\n");	
		}
		imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
	    image = vi.getContext().getResources().getDrawable(imageResource);
	    holder.ivSecond.setImageDrawable(image);
	  //  System.out.println("after set holder Image- track #" + position+1 + "subTrack #2 type Bus/Walk\n");
	}
	
	//hiding from view second sub-track which does not exist in the specific track
	else{
		holder.ivSecond.setVisibility(View.GONE);
		holder.ivArrowFirst.setVisibility(View.GONE);
		holder.tvDepArrTime.setGravity(View.TEXT_ALIGNMENT_TEXT_START);
		
	}
	if(subTracksNum > 2)
	{
		if((Track.SubTrackWalk.class).isInstance(tracks.get(position).getSubTracks().get(2)))
		{
			Track.SubTrackWalk subTrackWalk3 = (SubTrackWalk) tracks.get(position).getSubTracks().get(2);
			holder.tvThird.setText(subTrackWalk3.getDuration());
			uri = "drawable/"+ "walk_transit";
			//System.out.println("after set holder Text- track #" + position+1 + "subTrack #3 type Walk\n");
		}
		else
		{
			Track.SubTrackBus subTrackBus3 = (SubTrackBus) tracks.get(position).getSubTracks().get(2);
			if(subTrackBus3.getLineShortName().equals(""))
			{
				holder.tvThird.setText(subTrackBus3.getVehicleType());
				uri = "drawable/"+ "train_transit";			
			}
			else
			{
				holder.tvThird.setText(context.getResources().getString(R.string.line) +" "+ subTrackBus3.getLineShortName());
				uri = "drawable/"+ "bus_transit";
			}
		//	System.out.println("after set holder Text- track #" + position+1 + "subTrack #3 type Bus\n");
			
		}
		imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
	    image = vi.getContext().getResources().getDrawable(imageResource);
	    holder.ivThird.setImageDrawable(image);
	 //   System.out.println("after set holder Image- track #" + position+1 + "subTrack #3 type Bus/Walk\n");
	}
	//hiding from view third sub-track which does not exist in the specific track 
	else
	{
		holder.ivThird.setVisibility(View.GONE);
		holder.ivArrowSecond.setVisibility(View.GONE);	
	}
	
	//making the hidden arrow visible to imply there are more (than 3) sub-tracks...
	if(subTracksNum > 3)
		holder.ivArrowHidden.setVisibility(View.VISIBLE);
	
	holder.tvDepArrTime.setText(tracks.get(position).getDepArrTime());
	holder.tvDuration.setText(tracks.get(position).getTotalDuration());
	//System.out.println("after set holder Text- track #" + position+1 + "total time and duration\n");
	
	return vi;
	
	}
	
	/*
	 * 
	 * */
	static class ViewHolder{
		
		//text - busLine/walkTime
		TextView tvFirst;
		TextView tvSecond;
		TextView tvThird;
		
		TextView tvDepArrTime;
		TextView tvDuration;
		
		//image - busIcon/TrainIcon/walkIcon
		ImageView ivFirst;
		ImageView ivSecond;
		ImageView ivThird;
		ImageView ivArrowFirst;
		ImageView ivArrowSecond;
		ImageView ivArrowHidden;
		
	}
	
}
